<template>

  <div class="app-container">

    <el-form :inline="true" :model="queryForm" class="demo-form-inline" >
      <el-form-item style="margin-bottom:10px;">
        <!-- <el-button type="primary" icon="el-icon-plus"  :loading="loading" @click="handleAdd" plain>添加节点</el-button> -->
        <el-button type="danger" icon="el-icon-switch-button"   :loading="loading" @click="" plain>重启</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      border
      empty-text="暂无数据."
      style="width: 100%">
      <el-table-column
        type="selection"
        width="40">
      </el-table-column>
      <el-table-column
            type="index"
            label="序号"
            width="50"
            align="center"
            :index="indexMethod">
          </el-table-column>

      <el-table-column
        prop="name"
        label="名称"
        >
      </el-table-column>


      <el-table-column
        prop="ip"
        label="IP"
        >
      </el-table-column>


      <el-table-column
        prop="port"
        label="端口"
        >
      </el-table-column>

      <el-table-column prop="type" label="类型"  >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type='1' " >Master</el-tag>
          <el-tag v-else type="danger">node</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="password" label="口令"  >
      </el-table-column>

      <el-table-column prop="limit" label="限制路数"  >
      </el-table-column>

<!--      <el-table-column label="操作" align="left" >
       <template slot-scope="scope">
         <el-button type="text" icon="el-icon-edit-outline" size="small" @click="handleEdit(scope.row)">编辑
         </el-button> 
         <el-button  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
         </el-button>
       </template>
      </el-table-column> -->
    </el-table>

    <el-row>
      <el-col :span="24">
        <div align="right" style="margin-top: 0.3125rem;">
          <el-pagination background layout="prev, pager, next" :current-page="page.cur" :page-size="page.limit" :total="page.total" @current-change="handleCurrentChange" />
        </div>
      </el-col>
    </el-row>

    <!--编辑框-->
    <info-dialog
      v-if="editDialog.visible"
      :properties="editDialog"
      @closed="close"
      @refresh="load"
    />

  </div>
</template>

<script>
  import InfoDialog from './components/InfoDialog.vue'


  import {
    hasPermission
  } from '@/utils/permission'
  import {
    findClientList,
    createClient,
    deleteClient,
    restartClient,
    stopClient
  } from '@/api/cloud/client'
  import {
    isOk
  } from '@/utils/request'
  export default {
    name:'Project',
    components: {
      InfoDialog,
    },
    data() {
      return {
        loading:false,
        queryForm: {
          keywords: undefined,
        },
        page: {
          cur: 1,
          limit: 50,
          total: 0
        },
        tableData: [],
        editDialog: {
          visible: false,
          type: '0',
          data: {}
        },
      }
    },
    created() {
      this.load();
    },
    methods: {
      indexMethod(index) {
        return index +1;
      },
      onQuerySubmit(event) {
        this.load()
      },
      onReset(event) {
        this.queryForm.keywords=undefined;
        this.queryForm.status='';
        this.load()
      },
      load(){
        this.loading=true
        findClientList({
          cur: this.page.cur,
          limit: this.page.limit,
          keywords: this.queryForm.keywords,
        }).then(rsp => {
          if(isOk(rsp)){
            const _data = rsp.result
            this.page.total = _data.totalRow
            this.tableData = _data.list
            this.loading = false
          }
        }).catch(err => {
          console.log(err);
        })
        .finally(() => {
          this.loading = false
        })
      },
      close: function(e) {
        this.editDialog.visible = false
        this.editDialog.data = {}
        this.load()
      },
      handleCurrentChange(cur) {
        this.page.cur = cur
        this.load()
      },
      handleAdd: function(e) {
        this.editDialog.visible = true
        this.editDialog.type = '0'
        this.editDialog.data = {}
      },
      handleEdit: function(data) {
        this.editDialog.visible = true
        this.editDialog.data = data
        this.editDialog.type = '1'
      },
      handleDelete: function(data) {
        this.$confirm('若服务正在运行，将自动停止，确认删除【' + data.name + '】吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          deleteClient({
            id: data.id
          }).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 500
            })
            this.load()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
        })
      },

      handleStop: function(data) {
        this.$confirm('重启服务，将会影响正在使用该服务的用户，确认重启【' + data.name + '】吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          stopServer({
            id: data.id
          }).then(response => {
            this.$message({
              message: '成功',
              type: 'success',
              duration: 500
            })
            this.load()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
        })
      },
    }
  }
</script>
